package com.emep.changzhi.analyse.service.equipment;

import com.emep.changzhi.analyse.entity.equipment.EquipmentMoniter;
import com.emep.changzhi.analyse.repository.equipment.EquipmentMoniterRepository;
import com.emep.changzhi.analyse.utils.MapUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author yangj
 * @Description: 监测设备业务层，依赖持久层
 * @date 2018/2/24  9:54
 * @version V1.0
 */
@Slf4j
@Service
@Transactional
public class EquipmentMoniterService {

    @Resource
    private EquipmentMoniterRepository equipmentMoniterRepository;

    /**
     *新增监测设备，如果想修改，需要传一个id
     */
    public EquipmentMoniter saveEquipmentMoniter(Map<String , Object> payload){
        EquipmentMoniter equipment = new EquipmentMoniter();
        try {
            equipment = (EquipmentMoniter) MapUtils.convertMap(EquipmentMoniter.class , payload);
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return equipmentMoniterRepository.save(equipment);
    }

    /**
     * 查询所有
     * */
    public List<EquipmentMoniter> getEquipmentMoniterList() {
        return equipmentMoniterRepository.findAll();
    }

    /**
     * 没有条件的分页查询
     * */
    public Page<EquipmentMoniter> findEquipmentMoniterNoCriteria(Integer page, Integer size) {
        Pageable pageable = new PageRequest(page, size, Sort.Direction.ASC, "createDate");
        return equipmentMoniterRepository.findAll(pageable);
    }


    /**
     * 按条件分页查询
     * @param page 页数
     * @param size 数量
     * @param equipmentMoniter 参数
     * @return
     */
    public Page<EquipmentMoniter> findBookCriteria(Integer page, Integer size, EquipmentMoniter equipmentMoniter) {
        //规格定义//页码：前端从1开始，jpa从0开始，做个转换
        Pageable pageable = new PageRequest(page-1,size , Sort.Direction.ASC, "createDate");
        Page<EquipmentMoniter> equipmentPage = equipmentMoniterRepository.findAll(new Specification<EquipmentMoniter>(){
            @Override
            public Predicate toPredicate(Root<EquipmentMoniter> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                Predicate p1 = criteriaBuilder.equal(root.get("monitorTyptId").as(String.class), equipmentMoniter.getMonitorTyptId());
                query.where(criteriaBuilder.or(p1));
                return query.getRestriction();
            }
        },pageable);
        return equipmentPage;
    }

    /**
     * 根据id查询监测设备
     * */
    public EquipmentMoniter findEquipmentMoniterById(String id) {

        return equipmentMoniterRepository.findEquipmentMoniterById(id);
    }

    /**
     * 根据id删除监测设备
     * */
    public Boolean delete(String id) {
        Boolean result = false;
        try{
            Integer r = equipmentMoniterRepository.deleteEquipmentMoniterById(id);
            if(r == 1){
                result = true;
            }
        }catch (Exception e){
            log.error(e.getLocalizedMessage());
        }
        return result;

    }

}
